package com.ajk.server.dao.provider;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.ajk.server.model.BodypartDict;
import com.ajk.server.model.query.BodypartDictQuery;
import com.ajk.server.utils.IntegerUtil;
/**
 * 身体部位  <br>
 * dict_bodypart <br>
 * v1.0 2016-05-01
 * @see com.ajk.server.dao.BodypartDictMapper
 */
 public class BodypartDictProvider {
 
		/**
	 	* 新增 
	 	*/
	 	public String insert(BodypartDict domain){
			StringBuilder sb = new StringBuilder();
			
			sb.append("insert into dict_bodypart(create_time,last_modify_time");
		 	if(domain.getName()!=null){
				sb.append(",name");
			}
		 	if(domain.getParentId()!=null){
				sb.append(",parent_id");
			}
		 	if(domain.getSex()!=null){
				sb.append(",sex");
			}
		 	if(domain.getDescription()!=null){
				sb.append(",description");
			}
		 	if(domain.getInUsed()!=null){
				sb.append(",in_used");
			}
		 	if(domain.getSeq()!=null){
				sb.append(",seq");
			}
		 	if(domain.getIsSystem()!=null){
				sb.append(",is_system");
			}
			
			sb.append(") values ( now(),now() ");
			
		 	if(domain.getName()!=null){
				sb.append(",#{name}");
			}
		 	if(domain.getParentId()!=null){
				sb.append(",#{parentId}");
			}
		 	if(domain.getSex()!=null){
				sb.append(",#{sex}");
			}
		 	if(domain.getDescription()!=null){
				sb.append(",#{description}");
			}
		 	if(domain.getInUsed()!=null){
				sb.append(",#{inUsed}");
			}
		 	if(domain.getSeq()!=null){
				sb.append(",#{seq}");
			}
		 	if(domain.getIsSystem()!=null){
				sb.append(",#{isSystem}");
			}
			sb.append(")");
			
			return sb.toString();
		}

		/**
	 	* 更新 
	 	*/
		public String update(BodypartDict domain){
			StringBuilder sb = new StringBuilder();
			
			sb.append("update dict_bodypart set last_modify_time = now() ");
			
			if(domain.getName()!=null){
				sb.append(",name=#{name}");
			}
			if(domain.getParentId()!=null){
				sb.append(",parent_id=#{parentId}");
			}
			if(domain.getSex()!=null){
				sb.append(",sex=#{sex}");
			}
			if(domain.getDescription()!=null){
				sb.append(",description=#{description}");
			}
			if(domain.getInUsed()!=null){
				sb.append(",in_used=#{inUsed}");
			}
			if(domain.getSeq()!=null){
				sb.append(",seq=#{seq}");
			}
			if(domain.getIsSystem()!=null){
				sb.append(",is_system=#{isSystem}");
			}
			
			sb.append(" where id=#{id}");
			
			return sb.toString();
		}
		
		public String batchInsert(Map<String, List<BodypartDict>> map){
			StringBuffer sb = new StringBuffer();
			List<BodypartDict> bodypartDicts = map.get("list");
			sb.append("insert into dict_bodypart(create_time,last_modify_time,name,parent_id,sex,description,in_used,seq,is_system) values");
		    MessageFormat messageFormat = new MessageFormat("("
		    		+ "now(),now()"
		    		+ ",#'{'list[{0}].name}"
		    		+ ",#'{'list[{0}].parentId}"
		    		+ ",#'{'list[{0}].sex}"
		    		+ ",#'{'list[{0}].description}"
		    		+ ",#'{'list[{0}].inUsed}"
		    		+ ",#'{'list[{0}].seq}"
		    		+ ",#'{'list[{0}].isSystem}"
		    		+")");
		    		
		    if(CollectionUtils.isNotEmpty(bodypartDicts)) {
				sb.append(messageFormat.format(new Integer[]{0}));
				for (int i = 1; i <  bodypartDicts.size(); i++) {
					sb.append(",").append(messageFormat.format(new Integer[]{i}));
				}
			}
			return sb.toString();
		}
		
	/**
 	*通过query 查询 身体部位
 	*/
	public String findByQuery(BodypartDictQuery query){
		StringBuffer sb = new StringBuffer();
		sb.append("select *,(select name from dict_bodypart where id=a.parent_id ) as parent_name from dict_bodypart a where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getParentId())){
			sb.append(" and a.parent_id=#{parentId} ");
		}
		
		if(StringUtils.isNotEmpty(query.getKeyword())){
			sb.append(" and a.name like #{keyword}");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getInUsed())){
			if(query.getInUsed()==1){
				sb.append(" and a.in_used=1 ");
			}else{
				sb.append(" and a.in_used!=1 ");
			}
		}
		
		if(StringUtils.isEmpty(query.getSortStatment())){
			sb.append(" order by parent_id,seq desc ");
		}else{
			sb.append(" ").append(query.getSortStatment());
		}
		if(query.getStart()!=null && query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{start},#{limit}");
		}else if(query.getLimit()!=null && query.getLimit()!=0){
			sb.append(" limit #{limit}");
		}
		return sb.toString();
	}
	
	public String countByQuery(BodypartDictQuery query){
		StringBuffer sb = new StringBuffer();
		
		sb.append("select count(1) from dict_bodypart a where 1>0 ");
		
		if(IntegerUtil.isPositiveInteger(query.getParentId())){
			sb.append(" and a.parent_id=#{parentId} ");
		}
		
		if(StringUtils.isNotEmpty(query.getKeyword())){
			sb.append(" and a.name like #{keyword}");
		}
		
		if(IntegerUtil.isPositiveInteger(query.getInUsed())){
			if(query.getInUsed()==1){
				sb.append(" and a.in_used=1 ");
			}else{
				sb.append(" and a.in_used!=1 ");
			}
		}
		return sb.toString();
	}
	
	/**
 	*通过ids 查询 身体部位
 	*/
 	public String findByIds(Map<String, List<Integer>> map){
		StringBuffer sb = new StringBuffer();
		List<Integer> ids = map.get("list");
		sb.append("select * from dict_bodypart where ");
		if(ids==null || ids.isEmpty()){
			sb.append(" 1<2");
		}else{
			if(ids.size()==1){
				sb.append(" id=").append("#{list[0]}");
			}else{
				sb.append(" id in (").append("#{list[0]}");
				for(int i=1; i<ids.size() ; i++){
					sb.append(",").append("#{list[").append(i).append("]}");
				}
				sb.append(")");
			}
		}
		return sb.toString();
	}
	
 }	